#region (c) 2014 THN Solutions LLC. - All rights reserved
/*
THN WEB SERVER

Copyright (c) 2014, THN Solutions LLC. ( www.ThnSolutions.com )
Author: Nguyễn, M. Hải                 ( www.Minh-Hai.com     )
All rights reserved.

This library is dual-licensed.
  + For use in commercial software, please obtain a commercial license from THN Solutions LLC.    

  + For use in free software, this library is licensed under GPL version 3. A summary of 
	GPLv3 is listed below. You should also find a copy of GPLv3 as file License_GPLv3.txt 
	included with the source-code files.

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License version 3 as published by
    the Free Software Foundation  of the License.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
#endregion

#region Record of Change
//             							CHANGE HISTORY
// -------------------------------------------------------------------------------------
// |   DATE    | DEVELOPER  | DESCRIPTION                                              |
// -------------------------------------------------------------------------------------
// | 14-Nov-12 | HaiNM      | First creation.                                          |
// -------------------------------------------------------------------------------------
//
#endregion

#region Record of Audit
//             							REVIEW HISTORY
// -------------------------------------------------------------------------------------
// |   DATE    | REVIEWER   | NOTES                                                    |
// -------------------------------------------------------------------------------------
// -------------------------------------------------------------------------------------
//
#endregion

#region Using directives
using System;
using System.IO;

using Thn;
using Thn.Net;
using Thn.Net.Http;
using Thn.Collections;
#endregion

namespace Thn.Web
{
    /// <summary>
    /// 
    /// </summary>
#if DEBUG || TESTING
    public
#else
    internal
#endif
 class ExtensionRequestRouter : IHttpRequestHandler
    {
        #region Fields
        StringDictionary<IHttpRequestHandler> mHandlers = new StringDictionary<IHttpRequestHandler>();
        #endregion

        #region Properties
        #endregion

        #region Add Handler
        /// <summary>
        /// Register a new handler that will be responsible for processing a certain file extension.
        /// </summary>
        /// <param name="fileExtension">The file extension (eg: mp3, mp4, avi)</param>
        /// <param name="handler">The handler that will be used for handling that type of file.</param>
        public void AddHandler(string fileExtension, IHttpRequestHandler handler)
        {
            //pre-processing
            fileExtension = fileExtension.ToLowerInvariant();
            if (fileExtension.Contains(".")) fileExtension = fileExtension.Replace(".", "");

            //add to cache for later retrieval
            mHandlers[fileExtension] = handler;
        }
        #endregion

        #region IHttpRequestHandler Members

        #region Process
        void IHttpRequestHandler.Process(IHttpRequestHandlingContext context)
        {
            string path = context.Request.Path;
            string extension = Path.GetExtension(path);

            if (!string.IsNullOrEmpty(extension))
            {                
                #region Has extension

                //retrieve the appropriate handler
                extension = extension.ToLowerInvariant();
                IHttpRequestHandler handler = mHandlers[extension];

                //use the handler to process
                if (handler != null) handler.Process(context);
                #region Defensive tracing
                else
                {
                    //handler not found, skip
                }
                #endregion

                #endregion
            }
            else
            {
                #region No extension -> skip
                #endregion
            }
        }
        #endregion

        #endregion

        #region Constructors
        /// <summary>
        /// Default constructor.
        /// </summary>
        public ExtensionRequestRouter()
        { }
        #endregion
    }
}
